﻿using Infragistics.Win.Misc;
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace VoiceDeom
{
  public class VoiceEventHelper
  {
    public static void AddVoiceEvent(Control.ControlCollection controls)
    {
      foreach (var control in controls)
      {
        if (control is UltraButton)
        {
          var button = control as UltraButton;

          button.MouseHover += Button_MouseHover;
        }
        else if (control is MenuStrip)
        {
          var menu = control as MenuStrip;

          foreach (ToolStripDropDownItem menuItem in menu.Items)
          {
            AddMenuItemEvent(menuItem);
          }
        }

      }
    }

    static void AddMenuItemEvent(ToolStripDropDownItem dropDownItem)
    {
      dropDownItem.MouseHover += MenuItem_MouseHover;

      foreach (ToolStripDropDownItem item in dropDownItem.DropDownItems)
      {
        AddMenuItemEvent(item);
      }
    }

    static void MenuItem_MouseHover(object sender, EventArgs e)
    {
      var menuItem = sender as ToolStripItem;

      SpeakAsync(menuItem.Text);
    }

    static void Button_MouseHover(object sender, EventArgs e)
    {
      var button = sender as UltraButton;

      SpeakAsync(button.Text);
    }

    static void SpeakAsync(string content)
    {
      content = Regex.Replace(content, @"[^\w ]", "");

      VoiceHelper.SpeakAsync(content);
    }
  }
}